<script setup>
import { Back } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'

import { useTableList } from '@/hooks/useTableList'
import { fetchGnssPoint } from '@/api/manager/dataManage/deviceManage.js'
import SensorList from './sensorList.vue'

defineOptions({
  name: 'PointList',
})

const props = defineProps({
  parkId: {
    type: String,
    required: true,
  },
  currentType: {
    type: String,
    default: '',
  },
  parkList: {
    type: Array,
    default: [],
  },
})

const emit = defineEmits(['back'])

const showSensorList = ref(false)

const searchParams = ref({
  parkId: '',
  keyword: '',
})

const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, fetchGnssPoint, null, 'gnss')

const onBackFromSensorList = () => {
  showSensorList.value = false
}

const handleItemClick = (data) => {
  showSensorList.value = true
}

const handleBack = () => {
  emit('back')
}

onMounted(() => {
  searchParams.value.parkId = props.parkId
  initTableList(1)
})
</script>

<template>
  <div
    v-show="!showSensorList"
    class="search search1"
    :class="{ active: toggleSearchStatus }"
  >
    <div class="search-left" ref="searchFromRef">
      <el-form class="search-form" inline>
        <el-form-item label="关键字">
          <el-input
            class="search-input"
            v-model="searchParams.keyword"
            placeholder="请输入关键字"
            :suffix-icon="Search"
          ></el-input>
        </el-form-item>
      </el-form>
      <a
        class="search-left-toggle"
        v-if="showToggle"
        @click="handleToggleSearch"
      >
        <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
        {{ toggleSearchStatus ? '收起' : '展开' }}
      </a>
    </div>
    <div class="search-right">
      <el-button type="primary" @click="initTableList(1)"
        ><svg-icon name="search" size="14px" />搜索</el-button
      >
      <el-button @click="handleReset(null, ['parkId'])"
        ><svg-icon name="reset" size="17px" />重置</el-button
      >
    </div>
  </div>
  <div v-show="!showSensorList" class="content">
    <div class="content-operate" style="justify-content: flex-start">
      <el-button type="primary" @click="handleBack">
        <el-icon><Back /></el-icon>
        <span>返回项目列表</span>
      </el-button>
    </div>

    <div class="content-table">
      <el-table
        class="custom-table"
        :data="tableData"
        show-overflow-tooltip
        :tooltip-options="{ showAfter: 300 }"
      >
        <el-table-column type="index" label="序号" width="60px" />
        <el-table-column prop="pointName" label="点位名称">
          <template #default="scope">
            <el-link
              type="success"
              underline
              @click="handleItemClick(scope.row)"
              >{{ scope.row.pointName }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="昵称" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <!-- <el-table-column label="操作" align="center" fixed="right" width="80px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleEdit(scope.row, '编辑点位')"
              >编辑</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <div class="content-page">
      <el-pagination
        class="custom-pagination"
        :current-page="paginationParams.pageIndex"
        :page-size="paginationParams.pageSize"
        background
        :page-sizes="[10, 20, 30, 50]"
        layout="total, prev, pager, next, sizes, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="initTableList"
      />
    </div>
  </div>

  <SensorList
    v-if="showSensorList"
    :park-id="parkId"
    :current-type="currentType"
    :park-list="parkList"
    @back="onBackFromSensorList"
  />
</template>
